package com.ecnu.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.entity.SysUser;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.system.domain.ProjectQA;
import com.ecnu.system.domain.dto.ProjectQADTO;
import com.ecnu.system.service.IProjectQAService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "答疑解惑")
@RestController
@RequestMapping("/project/qa")
public class ProjectQAController extends BaseController {

    @Autowired
    private IProjectQAService projectQAService;

    @GetMapping("/list")
    @ApiModelProperty(value = "答疑解惑列表")
    public AjaxResult getList(
//            Integer id,
            String proId) {
        QueryWrapper<ProjectQA> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_id", proId);
        wrapper.eq("del_flag", "0");
//        wrapper.eq("id", id);
        ProjectQA one = projectQAService.getOne(wrapper);
        if (one == null) {
            ProjectQA projectQA = new ProjectQA();
            projectQA.setProId(proId);
            return AjaxResult.success(projectQA);
        }
        return AjaxResult.success(one);
    }

//    @PostMapping("/add")
//    @ApiModelProperty(value = "添加答疑解惑")
//    public AjaxResult addProjectQA(@Valid @RequestBody ProjectQADTO projectQADTO) {
//        LoginUser loginUser = getLoginUser();//获取当前登录用户信息
//        if (loginUser == null) {
//            return AjaxResult.error("请先登录");
//        }
//        SysUser user = loginUser.getUser();
//        if (user == null) {
//            return AjaxResult.error("用户信息为空");
//        }
//
//        return projectQAService.save(projectQADTO, user.getNickName()) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
//    }

    @PostMapping("/addText")
    @ApiModelProperty(value = "添加答疑解惑")
    public AjaxResult addText(@Valid @RequestBody ProjectQADTO projectQADTO) {
        LoginUser loginUser = getLoginUser();//获取当前登录用户信息
        if (loginUser == null) {
            return AjaxResult.error("请先登录");
        }
        SysUser user = loginUser.getUser();
        ProjectQA one = projectQAService.getOne(new QueryWrapper<ProjectQA>().eq("pro_id", projectQADTO.getProId()));
        if (one != null) {
            projectQADTO.setId(one.getId());
            return projectQAService.update(projectQADTO, user.getNickName()) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        }
        return projectQAService.addText(projectQADTO, user.getNickName()) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

//    @PostMapping("/update")
//    @ApiModelProperty(value = "修改答疑解惑")
//    public AjaxResult updateProjectQA(@Valid @RequestBody ProjectQADTO projectQADTO) {
//        LoginUser loginUser = getLoginUser();//获取当前登录用户信息
//        if (loginUser == null) {
//            return AjaxResult.error("请先登录");
//        }
//        SysUser user = loginUser.getUser();
//        if (user == null) {
//            return AjaxResult.error("用户信息为空");
//        }
//        if (projectQADTO.getId() == 0 || projectQADTO.getProId() == null) {
//            return AjaxResult.error("更新,id不能为空");
//        }
//        return projectQAService.update(projectQADTO, user.getNickName()) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
//    }

    @PostMapping("/updateText")
    @ApiModelProperty(value = "修改答疑解惑")
    public AjaxResult updateText(@Valid @RequestBody ProjectQADTO projectQADTO) {
        LoginUser loginUser = getLoginUser();//获取当前登录用户信息
        if (loginUser == null) {
            return AjaxResult.error("请先登录");
        }
        SysUser user = loginUser.getUser();
        if (user == null) {
            return AjaxResult.error("用户信息为空");
        }
        if (projectQADTO.getId() == 0 || projectQADTO.getProId() == null) {
            return AjaxResult.error("更新,id不能为空");
        }
        return projectQAService.updateText(projectQADTO, user.getNickName()) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

//    @GetMapping("/info")
//    @ApiModelProperty(value = "答疑解惑详情")
//    public AjaxResult info(String id) {
//        ProjectQA projectQA = projectQAService.getById(id);
//        return AjaxResult.success(projectQA);
//    }

//    @PostMapping("/del")
//    @ApiModelProperty(value = "删除答疑解惑")
//    public AjaxResult delProjectQA(@RequestBody int[] ids) {
//        LoginUser loginUser = getLoginUser();//获取当前登录用户信息
//        if (loginUser == null){
//            return AjaxResult.error("请先登录");
//        }
//        SysUser user = loginUser.getUser();
//        if (user == null){
//            return AjaxResult.error("用户信息为空");
//        }
//        if (ids.length==0){
//            return AjaxResult.error("删除,id不能为空");
//        }
//        return projectQAService.remove(ids, user.getNickName()) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
//    }


}
